﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Newtonsoft.Json;
using ProjScan.ScanRule.Attr;
using ProjScan.Submodules.CS_Check;
using ProjScan.Tools;

namespace ProjScan.ScanRule.CodeAnalysis
{
	// Token: 0x02000218 RID: 536
	[CSCheckRuleAttr]
	internal class InvalidDomainUsage : InstructionRuleImpBase
	{
		// Token: 0x06000F52 RID: 3922 RVA: 0x00007789 File Offset: 0x00005989
		public InvalidDomainUsage()
		{
			this.detectDomains = this.GetTopLevelDomainList();
			ScanLog.UnityLog("检测的域名有 " + string.Join(",", this.detectDomains));
		}

		// Token: 0x06000F53 RID: 3923 RVA: 0x0004437C File Offset: 0x0004257C
		protected override bool AnalyzeInst(MethodDefinition method, Instruction inst, ref LineMapping failedInfo)
		{
			bool flag = inst.OpCode == OpCodes.Ldstr;
			if (flag)
			{
				string text = inst.Operand as string;
				foreach (string text2 in this.detectDomains)
				{
					bool flag2 = Regex.IsMatch(text, "https?:\\/\\/(.*\\.)?" + Regex.Escape(text2));
					if (flag2)
					{
						failedInfo.Merge(LineMapping.GetMapping(method, inst));
						return false;
					}
				}
			}
			return true;
		}

		// Token: 0x17000359 RID: 857
		// (get) Token: 0x06000F54 RID: 3924 RVA: 0x00044428 File Offset: 0x00042628
		private string cacheDir
		{
			get
			{
				return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "GameDomain");
			}
		}

		// Token: 0x06000F55 RID: 3925 RVA: 0x0004444C File Offset: 0x0004264C
		private string Get(string url)
		{
			string result = string.Empty;
			try
			{
				HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
				HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
				StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
				result = streamReader.ReadToEnd();
				httpWebResponse.Close();
			}
			catch (Exception arg)
			{
				ScanLog.UnityLog(string.Format("请求 {0} 出错 {1}", url, arg));
			}
			return result;
		}

		// Token: 0x06000F56 RID: 3926 RVA: 0x000444C8 File Offset: 0x000426C8
		private List<InvalidDomainUsage.GameDomainInfo> GetAllGameDomain(int group)
		{
			string url = Setting.gameDomainBaseUrl+ (group + 1).ToString();
			string text = this.Get(url);
			bool flag = string.IsNullOrEmpty(text);
			List<InvalidDomainUsage.GameDomainInfo> result;
			if (flag)
			{
				result = this.ReadFromCache(group);
			}
			else
			{
				InvalidDomainUsage.AllGameDomainResponse allGameDomainResponse = JsonConvert.DeserializeObject<InvalidDomainUsage.AllGameDomainResponse>(text);
				bool flag2 = allGameDomainResponse.code == 0;
				if (flag2)
				{
					List<InvalidDomainUsage.GameDomainInfo> data = allGameDomainResponse.data;
					this.CheckAndCreateDirectory(this.cacheDir);
					this.SaveToFile(Path.Combine(this.cacheDir, string.Format("{0}.json", group)), JsonConvert.SerializeObject(data));
					result = data;
				}
				else
				{
					result = this.ReadFromCache(group);
				}
			}
			return result;
		}

		// Token: 0x06000F57 RID: 3927 RVA: 0x000077BF File Offset: 0x000059BF
		private void SaveToFile(string path, string content)
		{
			File.WriteAllText(path, content);
		}

		// Token: 0x06000F58 RID: 3928 RVA: 0x00044578 File Offset: 0x00042778
		private void CheckAndCreateDirectory(string path)
		{
			bool flag = !Directory.Exists(path);
			if (flag)
			{
				Directory.CreateDirectory(path);
			}
		}

		// Token: 0x06000F59 RID: 3929 RVA: 0x0004459C File Offset: 0x0004279C
		private List<InvalidDomainUsage.GameDomainInfo> ReadFromCache(int group)
		{
			string text = Path.Combine(this.cacheDir, string.Format("{0}.json", group));
			List<InvalidDomainUsage.GameDomainInfo> result = new List<InvalidDomainUsage.GameDomainInfo>();
			bool flag = File.Exists(text);
			if (flag)
			{
				string text2 = File.ReadAllText(text);
				result = JsonConvert.DeserializeObject<List<InvalidDomainUsage.GameDomainInfo>>(text2);
			}
			else
			{
				ScanLog.UnityLog("加载缓存 " + text + " 失败，没有找到对应的文件");
			}
			return result;
		}

		// Token: 0x06000F5A RID: 3930 RVA: 0x00044608 File Offset: 0x00042808
		private List<string> GetTopLevelDomainList()
		{
			HashSet<string> hashSet = new HashSet<string>();
			for (int i = 0; i <= 100; i++)
			{
				List<InvalidDomainUsage.GameDomainInfo> allGameDomain = this.GetAllGameDomain(i);
				bool flag = i >= 10 && allGameDomain.Count == 0;
				if (flag)
				{
					break;
				}
				foreach (InvalidDomainUsage.GameDomainInfo gameDomainInfo in allGameDomain)
				{
					Uri uri;
					bool flag2 = Uri.TryCreate(gameDomainInfo.address, UriKind.Absolute, out uri) && !string.IsNullOrEmpty(uri.Host);
					if (flag2)
					{
						string text = this.ExtractTopLevelDomain(uri.Host);
						hashSet.Add(text);
					}
				}
			}
			return new List<string>(hashSet);
		}

		// Token: 0x06000F5B RID: 3931 RVA: 0x000446EC File Offset: 0x000428EC
		private string ExtractTopLevelDomain(string host)
		{
			string[] array = host.Split(new char[]
			{
				'.'
			});
			bool flag = array.Length == 3;
			string result;
			if (flag)
			{
				result = array[1] + "." + array[2];
			}
			else
			{
				result = host;
			}
			return result;
		}

		// Token: 0x040008F8 RID: 2296
		private List<string> detectDomains;

		// Token: 0x02000219 RID: 537
		internal class AllGameDomainResponse
		{
			// Token: 0x040008F9 RID: 2297
			public int code;

			// Token: 0x040008FA RID: 2298
			public string msg;

			// Token: 0x040008FB RID: 2299
			public List<InvalidDomainUsage.GameDomainInfo> data;
		}

		// Token: 0x0200021A RID: 538
		internal class GameDomainInfo
		{
			// Token: 0x040008FC RID: 2300
			public string game_key;

			// Token: 0x040008FD RID: 2301
			public string game_name;

			// Token: 0x040008FE RID: 2302
			public string region;

			// Token: 0x040008FF RID: 2303
			public string biz;

			// Token: 0x04000900 RID: 2304
			public string address;
		}
	}
}
